package extract.knowledge.general;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Common on 7/31/16.
 */
public class NodeParser {
    static final Logger logger = LoggerFactory.getLogger(NodeParser.class);

    private static String couch_id_split = "-";
    private static String couch_value_split = "[\\[\\]]";  //[] 表示内部的任意字符之一都可作为分隔符，\\]是对正则表达式保留字符]进行转义
    public static Integer INVALID_ID = -1;

    public static String parse_label(String couch_id){
        String[] parsed = couch_id.split(couch_id_split);
        if(parsed.length < 2){
            logger.error("Invalid couchdb id. ignore");
            return "";
        }
        String label = parsed[0];
        return label;
    }

    public static Integer parse_id(String couch_id){
        String[] parsed = couch_id.split(couch_id_split);
        if(parsed.length < 2){
            logger.error("Invalid couchdb id. ignored!");
            return -1;
        }
        Integer id = Integer.parseInt(parsed[1]);
        return id;
    }


    public static String parse_string_zh(String value){
        if(value == null) value = "";
        String[] parsed = value.split(couch_value_split);
        //System.out.println("length = " + parsed.length);
        String zh;
        if(parsed.length >= 2) zh = parsed[1];
        else zh = parsed[0];
        return zh;
    }

    public static String parse_string_en(String value){
        if(value == null) value = "";
        String[] parsed = value.split(couch_value_split);
        return parsed[0];
    }


    /**************
     *
     * @param args
     */
    public static void main(String[] args){
        String couch_value = "People's Republic of China province-level subdivision templates[中国各省模板]";
        System.out.println(parse_string_zh(couch_value));
        //System.out.println(parse_string_en(couch_value));
    }




}
